#region Copyright (C) 2007 Jens Winter

//Copyright (C) 2007 Jens Winter

//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.

//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.

//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#endregion

using System;
using System.Linq;
using System.Xml.Linq;
using MbUnit.Framework;

namespace Flynn.Core.Tests
{
    [TestFixture]
    public class ProjectLoaderFixture
    {
        private ProjectLoader _projectLoader;

        [SetUp]
        public void SetUp()
        {
            _projectLoader = new ProjectLoader();
        }

        [Test]
        [ExpectedException(typeof(LoadingProjectException))]
        public void When_Loading_Project_File_Fails_Exception_Is_Thrown()
        {
            _projectLoader.LoadProject("UnknownProjectFile.xml");
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void When_Loading_ProjectDoc_ProjectDoc_Must_Not_Be_Null()
        {
            _projectLoader.LoadProject((XDocument)null);
        }

        [Test]
        [ExpectedException(typeof(LoadingProjectException))]
        public void When_Loading_ProjectDoc_Fails_Exception_Is_Thrown()
        {
            _projectLoader.LoadProject(new XDocument());
        }

        [Test]
        public void Project_Name_Is_Set()
        {
            XDocument projectDoc = XDocument.Parse(
                @"<Flynn Name=""FlynnDemo""/>"
                );

            Project project = _projectLoader.LoadProject(projectDoc);

            Assert.AreEqual("FlynnDemo", project.Name);
        }

        [Test]
        public void DatabaseType_Is_Set()
        {
            XDocument projectDoc = XDocument.Parse(
                @"<Flynn Name=""FlynnDemo"">
                    <Database>SqlServer</Database>
                  </Flynn>
                "
                );

            Project project = _projectLoader.LoadProject(projectDoc);

            Assert.AreEqual("SqlServer", project.Database);
        }

        [Test]
        public void VersioningType_Is_Set()
        {
            XDocument projectDoc = XDocument.Parse(
                @"<Flynn Name=""FlynnDemo"">
                    <Versioning>SqlServer.VersionsTable</Versioning>
                  </Flynn>
                "
                );

            Project project = _projectLoader.LoadProject(projectDoc);

            Assert.AreEqual("SqlServer.VersionsTable", project.Versioning);
        }

        [Test]
        public void Versions_Are_Set()
        {
            XDocument projectDoc = XDocument.Parse(
                @"<Flynn Name=""FlynnDemo"">
                    <Update Version=""1""/>
                    <Update Version=""2""/>
                    <Update Version=""3""/>
                  </Flynn>
                "
                );

            Project project = _projectLoader.LoadProject(projectDoc);

            Assert.AreEqual(3, project.Updates.Count());
        }

        [Test]
        public void Author_Of_Version_Is_Set()
        {
            XDocument projectDoc = XDocument.Parse(
                @"<Flynn Name=""FlynnDemo"">
                    <Update Version=""1"" Author=""Joe""/>
                  </Flynn>
                "
                );

            Project project = _projectLoader.LoadProject(projectDoc);

            Assert.AreEqual("Joe", project.Updates.ToList()[0].Author);
        }

        [Test]
        public void CreatedDate_Of_Version_Is_Set()
        {
            XDocument projectDoc = XDocument.Parse(
                @"<Flynn Name=""FlynnDemo"">
                    <Update Version=""1"" CreatedDate=""2007-12-24""/>
                  </Flynn>
                "
                );

            Project project = _projectLoader.LoadProject(projectDoc);

            Assert.AreEqual(new DateTime(2007, 12, 24), project.Updates.ToList()[0].CreatedDate);
        }

        [Test]
        public void Description_Of_Version_Is_Set()
        {
            XDocument projectDoc = XDocument.Parse(
                @"<Flynn Name=""FlynnDemo"">
                    <Update Version=""1"">
                        <Description>Description</Description>
                    </Update>
                  </Flynn>
                "
                );

            Project project = _projectLoader.LoadProject(projectDoc);

            Assert.AreEqual("Description", project.Updates.ToList()[0].Description);
        }

        [Test]
        public void SqlStatements_Of_Version_Are_Set()
        {
            XDocument projectDoc = XDocument.Parse(
                @"<Flynn Name=""FlynnDemo"">
                    <Update Version=""1"">
                        <SqlStatement>SqlStatement1</SqlStatement>
                        <SqlStatement>SqlStatement2</SqlStatement>
                        <SqlStatement>SqlStatement3</SqlStatement>
                    </Update>
                  </Flynn>
                "
                );

            Project project = _projectLoader.LoadProject(projectDoc);

            Assert.AreEqual(3, project.Updates.ToList()[0].SqlStatements.Count());
            Assert.AreEqual("SqlStatement1", project.Updates.ToList()[0].SqlStatements.ToList()[0]);
            Assert.AreEqual("SqlStatement2", project.Updates.ToList()[0].SqlStatements.ToList()[1]);
            Assert.AreEqual("SqlStatement3", project.Updates.ToList()[0].SqlStatements.ToList()[2]);
        }

        [Test]
        [ExpectedException(typeof(LoadingProjectException))]
        public void When_Loading_Project_Fails_Throw_LoadingProjectException()
        {
            _projectLoader.LoadProject("UnknownProjectFile.xml");
        }
    }
}